package com.infinit.easyreader.Biz;

import com.infinit.easyreader.entity.ReadInfo;
import com.infinit.easyreader.utils.CommonUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

/**
 * 包含类 : com.infinit.easyreader.Biz
 * 作用：阅读器的阅读功能
 * Created by rzy on 2016/2/18.
 */
public class ReadBiz {


    private RandomAccessFile raf;
    //最大文本数限制
    private int maxTextCount = 2000;
    private long fileLength;
    private int maxLine;
    private int wordPerLine = 25;

    public ReadBiz(String filePath) throws FileNotFoundException {
        raf = new RandomAccessFile(filePath, "r");
        fileLength = new File(filePath).length();
    }

    public void setMaxLine(int maxLine) {
        this.maxLine = maxLine;
    }

    public void setWordPerLine(int wordPerLine) {
        this.wordPerLine = wordPerLine;
    }

    public void setMaxTextCount(int maxTextCount) {
        this.maxTextCount = maxTextCount;
    }

    /**
     * 作用：阅读方法，用于阅读指定的目录下的书籍
     * 每次读入两个字节，如果是0x0D，判断下一位是否为0xOA，是则为一个换行符号
     *
     * @return 当前页面需要显示的数据
     */


    public String readBook() throws IOException {
        int i = 0;
        byte textByte[] = new byte[2 * maxTextCount];
        //按行读取，最后将所有的数据进行返回
        byte aByte;
        while (i < (maxTextCount * 2 > fileLength ? fileLength : maxTextCount)) {
            textByte[i] = raf.readByte();
            i++;
        }
        return CommonUtils.getEncodedString(textByte);
    }

    /**
     * 作用：重载阅读方法，根据每页显示行数来读取书籍
     *
     * @return 当前页面需要显示的数据
     */


    public ReadInfo readBook(long currentPosition) throws IOException {
        raf.seek(currentPosition);
        ReadInfo info = ReadInfo.newInsatance();
        byte[] textBytes;
        //判断下一次阅读是否会来到文件末端
        if ((fileLength - currentPosition - 1) > maxTextCount * 2) {
            textBytes = new byte[maxTextCount * 2];
            raf.read(textBytes);
            info.setIsEOF(false);

        } else {
            textBytes = new byte[(int) (fileLength - currentPosition - 1)];
            raf.read(textBytes);
            info.setIsEOF(true);
        }
        info.setReadEndPosition(raf.getFilePointer());
        info.setReadStartPosition(currentPosition);
        info.setReadDetail(CommonUtils.getEncodedString(textBytes));
        info.setPageSize(maxTextCount);
        return info;
    }


    /**
     * 阅读书籍的逻辑，按照行来进行阅读，根据回车来判断当前页面的可显示数
     *
     * @param startPosition 开始阅读的起点
     * @return 阅读的实体
     */

    public ReadInfo readBooks(long startPosition) throws IOException {

        raf.seek(startPosition);
        ReadInfo info = ReadInfo.newInsatance();
        List<Byte> byteList = new ArrayList<>();
        //逐行读取
        outer:
        for (int i = 0; i < maxLine; i++) {
            //按字节读取
            //如果遇到换行符则跳出循环
            for (int j = 0; j < wordPerLine * 2; j++) {
                //判断是否为\n\r
                byte b = raf.readByte();
                if (b == 13) {
                    byteList.add((byte) 10);
                    //换行
                    continue outer;
                } else if (b == 10) {
                } else {
                    byteList.add(b);
                }
            }
        }

        info.setPageSize(byteList.size());
        byte[] bytes = new byte[byteList.size()];
        int index = 0;
        for (Byte b : byteList) {
            bytes[index] = b;
            index++;
        }
        info.setReadDetail(CommonUtils.getEncodedString(bytes));
        info.setIsEOF(false);
        info.setReadStartPosition(startPosition);
        info.setReadEndPosition(raf.getFilePointer());
        return info;
    }


    /**
     * 作用：销毁randomAccessFile的实例
     */

    public void destroy() {
        try {
            raf.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
